<template>
	<view class="star-comment-reply" :style="{ bottom: keyboardHeight + 'px' }">
		<scroll-view v-if="imgList.length > 0" class="img-list-box" scroll-x enable-flex
			:scroll-into-view="'item' + intoViewCurrent" scroll-with-animation>
			<block v-for="(item, index) in imgList" :key="index">
				<view :id="'item' + index" class="img-item" @click="previewImg(imgList, index)">
					<image class="img" :src="item" mode="aspectFill"></image>
					<image class="remove-icon" :src="propOption.iconImgRemove" mode="" @click.stop="removeImg(index)">
					</image>
					<view class="img-num">
						{{ index + 1 }}
					</view>
				</view>
			</block>
		</scroll-view>
		<view class="reply-main">
			<view class="input-box" :class="
          propOption.showChooseImg && content.length > 10
            ? 'more-input-box'
            : ''
        ">
				<textarea v-model="content" @focus="onFocus" :focus="focus" :placeholder="placeholder" auto-height
					auto-blur :adjust-position="false" :cursor-spacing="50" fixed :show-confirm-bar="false"
					:maxlength="9999" :auto-height="lineCount < 5" @linechange="linechange"
					@blur="focus = false"></textarea>
				<view v-if="content" class="send-box" @click="sendComment">发送</view>
				<view v-else class="send-box-edit">
					<image class="edit-icon" :src="propOption.iconEdit"> </image>
				</view>
			</view>
			<view v-if="!content" class="footer-operation">
				<view v-if="propOption.showLike" class="footer-icon-wrapper" hover-stop-propagation
					@click.stop="toggleLike">
					<image v-if="postLikes[post.id].isLiked" class="icon scale-ver-center" :src="propOption.iconLiked">
					</image>
					<image v-else class="icon" :src="propOption.iconLike"> </image>
					<text class="text">
						{{ numberFormat(postLikes[post.id].like_num) }}
					</text>
				</view>
				<view v-if="propOption.showMessage" class="footer-icon-wrapper" hover-stop-propagation
					@click.stop="onReply">
					<image class="icon scale-ver-center" :src="propOption.messageIcon"> </image>
					<text class="text">
						{{ numberFormat(commentCount) }}
					</text>
				</view>
				<view v-if="propOption.showShare" class="footer-icon-wrapper" hover-stop-propagation>
					<button open-type="share">
						<image class="icon share-icon scale-ver-center" :src="propOption.share"> </image>
					</button>
				</view>
			</view>

			<!-- 选择图片 -->
			<!-- <image v-if="propOption.showChooseImg" class="item-icon" :src="propOption.iconImg" @click.stop="chooseImg">
			</image> -->
		</view>
	</view>
</template>

<script>
	import numberFormat from "../../utils/numberFormat.js";
	import {
		toLogin
	} from "@/utils/login";
	import {
		likePost
	} from "@/api/discover.js";
	import {
		mapGetters,
		mapMutations
	} from 'vuex'

	export default {
		props: {
			post: {
				type: Object,
				default: () => ({}),
			},
			option: {
				type: Object,
				default: () => ({}),
			}
		},
		data() {
			return {
				propOption: {
					showChooseImg: true,
					showLike: true,
					showShare: true,
					showMessage: true,
					imgMaxCount: 3, // 选择图片最大数量
					imgMaxSize: 5, // 单位：M
					iconImgRemove: "/uni_modules/star-community/static/remove.svg", // 移除图片icon
					iconImg: "/uni_modules/star-community/static/chooseImg.svg", // 选择图片icon
					iconLike: "/static/images/icon_collection.png",
					iconLiked: "/static/images/icon_collection_s.png",
					share: "/static/images/icon_share_black.png",
					messageIcon: "/static/images/icon-message.png",
					iconEdit: "/static/images/icon_edit2.png"
				},

				focus: false, // 评论框聚焦
				intoViewCurrent: 0, // 图片自动滑动到哪张
				content: "",
				imgList: [],
				placeholder: "留下评论，到此一游",
				lineCount: 0,
				keyboardHeight: 0, // 键盘高度
				to_father_id: null, // 回复父评论
				to_child_id: null, // 回复子评论
				to_user_id: null, // 回复谁
				commentCount: this.post?.comment_num || 0,
			};
		},
		created() {
			this.propOption = {
				...this.propOption,
				...this.option,
			};
			// #ifndef H5 || MP-BAIDU || MP-TOUTIAO
			uni.onKeyboardHeightChange((res) => {
				this.keyboardHeight = res.height;
			});
			// #endif
		},
		destroyed() {
			// #ifndef H5 || MP-BAIDU || MP-TOUTIAO
			uni.offKeyboardHeightChange();
			// #endif
		},
		computed: {
			...mapGetters(['postLikes']),
		},
		methods: {
			...mapMutations(['UPDATEPOSTLIKES']),
			numberFormat,
			linechange(e) {
				this.lineCount = e.detail.lineCount;
			},
			// 重置评论框
			resetCommentReply() {
				this.focus = false; // 评论框聚焦
				this.content = "";
				this.imgList = [];
				this.placeholder = "留下评论，到此一游";
				this.lineCount = 0;
				this.keyboardHeight = 0; // 键盘高度
				this.to_father_id = null; // 回复父评论
				this.to_child_id = null; // 回复子评论
				this.to_user_id = null; // 回复谁
			},
			// 点击喜欢评论
			toggleLike() {
				if (!this.isLogin) {
					return toLogin()
				}
				let _isLiked = this.postLikes[this.post.id].isLiked
				let _like_num = this.postLikes[this.post.id].like_num

				// 根据当前的 isLiked 状态来增加或减少点赞数量
				_like_num = _isLiked ? _like_num - 1 : _like_num + 1;
				_isLiked = !_isLiked;
				this.UPDATEPOSTLIKES([{
					id: this.post.id,
					isLiked: _isLiked,
					like_num: _like_num
				}])
				likePost({
					id: this.post.id,
					action: _isLiked ? 1 : 0
				});
				this.$emit("toggleLike", this.comment, this.isLiked);
			},
			onFocus() {
				if (!this.isLogin) {
					return toLogin()
				}
			},
			onReply() {
				if (!this.isLogin) {
					return toLogin()
				}
				this.resetCommentReply()
				this.focus = true
			},
			// 发送评论
			sendComment() {

				this.$emit("sendComment", {
					to_father_id: this.to_father_id,
					to_child_id: this.to_child_id,
					to_user_id: this.to_user_id,
					content: this.content,
					imgList: this.imgList,
				});
			},
			// 预览图片
			previewImg(urls, current) {
				uni.previewImage({
					urls,
					current,
				});
			},
		},
	};
</script>

<style scoped lang="scss">
	.star-comment-reply {
		position: fixed;
		width: 100%;
		z-index: 9;
		bottom: 0;
		transition: all 0.15s;

		.img-list-box {
			box-shadow: -4rpx -4rpx 12rpx -4rpx rgb(238, 238, 238);
			box-sizing: border-box;
			max-width: 670rpx;
			width: auto;
			float: right;
			white-space: nowrap;
			padding: 0 0 14rpx 30rpx;
			border-radius: 10rpx 0 0 0;
			background-color: #ffffff;

			.img-item {
				position: relative;
				display: inline-flex;
				flex-shrink: 0;
				margin: 30rpx 30rpx 0 0;
				background-color: #ffffff;

				.img {
					display: flex;
					flex-shrink: 0;
					height: 130rpx;
					width: 130rpx;
				}

				.remove-icon {
					height: 36rpx;
					width: 36rpx;
					position: absolute;
					top: -14rpx;
					right: -14rpx;
					border-radius: 50%;
					opacity: 0.8;
				}

				.img-num {
					position: absolute;
					bottom: 0;
					right: 0;
					color: #ffffff;
					background-color: #888888;
					font-size: 22rpx;
					padding: 2rpx 6rpx;
					border-radius: 4rpx 0 0 0;
					opacity: 0.8;
				}
			}
		}

		.share-icon {
			width: 30rpx;
			height: 28rpx;
			min-width: 30rpx;
			min-height: 28rpx;
		}

		.reply-main {
			width: 100%;
			padding: 16rpx 30rpx;
			box-sizing: border-box;
			display: inline-flex;
			align-items: center;
			background-color: #fff;

			.input-box {
				min-height: 56rpx;
				width: 100%;
				padding: 18rpx 30rpx;
				border-radius: 50rpx;
				background-color: #eee;
				box-sizing: border-box;
				display: inline-flex;
				align-items: center;
				position: relative;

				textarea {
					width: 100%;
				}

				.icon {
					margin-left: 10rpx;
					height: 50rpx;
				}

				.send-box-edit {
					position: absolute;
					right: 32rpx;
					top: 18rpx;

					.edit-icon {
						width: 20rpx;
						height: 20rpx;
					}
				}

				.send-box {
					position: absolute;
					right: 10rpx;
					height: 53rpx;
					width: 100rpx;
					margin-left: 20rpx;
					flex-shrink: 0;
					border-radius: 50rpx;
					background-color: #FF5E61;
					color: #fff;
					display: flex;
					align-items: center;
					justify-content: center;
					z-index: 99;
				}
			}

			.more-input-box {
				display: flex;
				flex-direction: column;
				align-items: flex-end;

				textarea {
					margin-bottom: 10rpx;
				}
			}

			.item-icon {
				margin-left: 20rpx;
				height: 72rpx;
				width: 72rpx;
				flex-shrink: 0;
			}
		}
	}

	.footer-icon-wrapper {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		margin-left: 24rpx;

		.text {
			margin-left: 8rpx;
		}
	}

	.footer-operation {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100%;
	}
</style>